home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 April / april_2001.iso / intercd / root / ^Palm / Games / eCross / src / LevelSelector.java < prev    next >
Encoding:
Java Source  |  2000-07-28  |  5.9 KB  |  238 lines

  1. /*
  2.  * LevelSelector.java - A selector for levels databases
  3.  * eCross is Copyright (C) 2000 Romain Guy
  4.  * guy.romain@bigfoot.com
  5.  * www.jext.org
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  */
  21.  
  22. import waba.fx.*;
  23. import waba.io.*;
  24. import waba.ui.*;
  25.  
  26. /**
  27.  * This is a level selector.
  28.  * @version 1.0
  29.  * @author Romain Guy <guy.romain@bigfoot.com>
  30.  */
  31.  
  32. public class LevelSelector extends Bounded
  33. {
  34.   private byte state = 0;
  35.   private boolean lGray, rGray;
  36.   private int currentLevel = 0, maxLevels;
  37.  
  38.   private Catalog levels;
  39.   private String dataBase, info;
  40.  
  41.   // arrows
  42.   private Image leftArrow = new Image("datas/larrow.bmp");
  43.   private Image leftGrayedArrow = new Image("datas/glarrow.bmp");
  44.   private Image rightArrow = new Image("datas/rarrow.bmp");
  45.   private Image rightGrayedArrow = new Image("datas/grarrow.bmp");
  46.  
  47.   /**
  48.    * Creates a new level selector.
  49.    * @param dataBase Defines the levels database
  50.    */
  51.  
  52.   public LevelSelector(String dataBase)
  53.   {
  54.     this.dataBase = dataBase;
  55.   }
  56.  
  57.   /**
  58.    * Handles a pen down event.
  59.    * @param parent The caller
  60.    * @param g The <code>Graphics</code> surface of the caller
  61.    * @param x The x coordinate of the event
  62.    * @param y The y coordinate of the event
  63.    * @return <code>true</code> if the event was handled
  64.    */
  65.  
  66.   public boolean handlePenEvent(eCross parent, Graphics g, int x, int y)
  67.   {
  68.     // levels switching
  69.     if (state == 1)
  70.     {
  71.       if (x > 13 && x < 43 && y > 97 && y < 112)
  72.       {
  73.  
  74.         state = 0;
  75.         levels.close();
  76.         parent.setLevel(currentLevel);
  77.         parent.levelFinished();
  78.         //parent.resumeTimer();
  79.  
  80.       } else if (x > 46 && x < 81 && y > 97 && y < 112) {
  81.  
  82.         state = 0;
  83.         levels.close();
  84.         parent.draw();
  85.         parent.resumeTimer();
  86.  
  87.       } else if (!lGray && x > 128 && x < 134 && y > 100 && y < 111) {
  88.  
  89.         if (--currentLevel == 0)
  90.         {
  91.           g.drawImage(leftGrayedArrow, 128, 125);
  92.           lGray = true;
  93.         }
  94.  
  95.         if (rGray)
  96.         {
  97.           g.drawImage(rightArrow, 140, 125);
  98.           rGray = false;
  99.         }
  100.  
  101.         paintLevel(g);
  102.  
  103.       } else if (!rGray && x > 140 && x < 146 && y > 100 && y < 111) {
  104.  
  105.         if (++currentLevel == maxLevels - 1)
  106.         {
  107.           g.drawImage(rightGrayedArrow, 140, 100);
  108.           rGray = true;
  109.         }
  110.  
  111.         if (lGray)
  112.         {
  113.           g.drawImage(leftArrow, 128, 100);
  114.           lGray = false;
  115.         }
  116.  
  117.         paintLevel(g);
  118.       }
  119.  
  120.       return true;
  121.     }
  122.  
  123.     if (x > this.x && x < this.x + this.width &&
  124.         y > this.y && y < this.y + this.height)
  125.     {
  126.       showSelector(parent, g);
  127.       return true;
  128.     }
  129.  
  130.     return false;
  131.   }
  132.  
  133.   /**
  134.    * Paints the level selector.
  135.    */
  136.  
  137.   private void showSelector(eCross parent, Graphics g)
  138.   {
  139.     parent.pauseTimer();
  140.  
  141.     levels = new Catalog(dataBase, Catalog.READ_ONLY);
  142.     if (!levels.isOpen())
  143.       return;
  144.     maxLevels = levels.getRecordCount();
  145.  
  146.     state = 1;
  147.     currentLevel = parent.getLevel();
  148.     lGray = (currentLevel == 0);
  149.     rGray = (currentLevel == maxLevels - 1);
  150.  
  151.     g.setColor(255, 255, 255);
  152.     // erase place of the window
  153.     g.fillRect(9, 54, 142, 62);
  154.  
  155.     g.setColor(0, 0, 0);
  156.     g.drawRect(10, 55, 140, 60);
  157.  
  158.     Font font = new Font("Helvetica", Font.BOLD, 10);
  159.     FontMetrics fm = parent.getFontMetrics(font);
  160.     g.setFont(font);
  161.  
  162.     g.drawRect(13, 97, 30, 15);
  163.     g.drawDots(15, 112, 43, 112);
  164.     g.drawDots(43, 112, 43, 99);
  165.     String text = new String("Ok");
  166.     g.drawText(text, 13 + (30 - fm.getTextWidth(text)) / 2, 99);
  167.  
  168.     g.drawRect(46, 97, 35, 15);
  169.     g.drawDots(48, 112, 81, 112);
  170.     g.drawDots(81, 112, 81, 99);
  171.     text = new String("Cancel");
  172.     g.drawText(text, 46 + 1 + (35 - fm.getTextWidth(text)) / 2, 99);
  173.  
  174.     g.drawImage(lGray ? leftGrayedArrow : leftArrow, 128, 100);
  175.     g.drawImage(rGray ? rightGrayedArrow : rightArrow, 140, 100);
  176.  
  177.     g.setFont(new Font("Helvetica", Font.PLAIN, 10));
  178.     paintLevel(g);
  179.   }
  180.  
  181.   // paints infos about current level
  182.  
  183.   public void paintLevel(Graphics g)
  184.   {
  185.     if (!levels.setRecordPos(currentLevel))
  186.       return;
  187.  
  188.     g.setColor(255, 255, 255);
  189.     g.fillRect(11, 56, 138, 40);
  190.  
  191.     g.setColor(0, 0, 0);
  192.     g.drawText(new StringBuffer("Level ").append(currentLevel + 1).toString(),
  193.                13, 67);
  194.  
  195.     byte[] b = new byte[2];
  196.     levels.readBytes(b, 0, b.length);
  197.  
  198.     StringBuffer buf = new StringBuffer("Best time: ");
  199.     if (b[0] < 10)
  200.       buf.append('0');
  201.     buf.append(b[0]).append(':');
  202.  
  203.     if (b[1] < 10)
  204.       buf.append('0');
  205.  
  206.     g.drawText(buf.append(b[1]).toString(), 13, 67 + 11);
  207.  
  208.     g.drawText((currentLevel + 1) + "/" + maxLevels, 95, 99);
  209.   }
  210.  
  211.   public void setInfo(String info)
  212.   {
  213.     this.info = info;
  214.   }
  215.  
  216.   public void paint(Control c, Graphics g)
  217.   {
  218.     Font font = new Font("Helvetica", Font.PLAIN, 10);
  219.     g.setFont(font);
  220.     FontMetrics fm = c.getFontMetrics(font);
  221.  
  222.     g.setColor(0, 0, 0);
  223.     g.drawText(info,
  224.                x + 1 + (this.width - fm.getTextWidth(info)) / 2,
  225.                2);
  226.   }
  227.  
  228.   public void free()
  229.   {
  230.     leftArrow.free();
  231.     leftGrayedArrow.free();
  232.     rightArrow.free();
  233.     rightGrayedArrow.free();
  234.   }
  235. }
  236.  
  237. // End of LevelSelector.java
  238.